import tkinter as tk
from tkinter import ttk


class MultiEntryDialog:
    def __init__(self, master, fields):
        self.master = master
        self.fields = fields
        self.entries = {}
        self.width = 320
        self.height = 200
        self.setup_ui()
        screen_width = self.master.winfo_screenwidth()
        screen_height = self.master.winfo_screenheight()
        x = int((screen_width / 2) - (self.width / 2))
        y = int((screen_height / 2) - (self.height / 2))
        self.master.geometry(f'{self.width}x{self.height}+{x}+{y}')

    def setup_ui(self):
        self.master.title("多输入对话框")

        # 创建输入框和标签
        for i, field in enumerate(self.fields):
            label = ttk.Label(self.master, text=field + ":")
            label.grid(row=i, column=0, padx=5, pady=5, sticky="e")

            entry = ttk.Entry(self.master)
            entry.grid(row=i, column=1, padx=5, pady=5, sticky="we")
            self.entries[field] = entry

        # 创建按钮
        button_frame = ttk.Frame(self.master)
        button_frame.grid(row=len(self.fields), column=0, columnspan=2, pady=10)

        ttk.Button(button_frame, text="确定", command=self.on_ok).pack(side="left", padx=5)
        ttk.Button(button_frame, text="取消", command=self.on_cancel).pack(side="left", padx=5)

    def on_ok(self):
        self.results = {field: entry.get() for field, entry in self.entries.items()}
        self.master.destroy()

    def on_cancel(self):
        self.results = None
        self.master.destroy()

    def show(self):
        self.master.wait_window()
        return self.results



